<?php


namespace app\common\model;


use think\Model;

class Notice extends Model
{
    public function addNotice($data){
        $result = $this->save($data);
        return $result;
    }
    public function getList($current_page=1,$num=10){
        $count = $this->count();
        $total = ceil($count/$num);
        $res = $this->order('id','desc')->limit(($current_page-1)*$num,$num)->select();
        $pageInfo = [
            "current_page"=>$current_page,
            "total"=>$total
        ];
        $result = [
            'pageInfo' =>$pageInfo,
            'list' => $res
        ];
        return $result;
    }
    public function getListByWhere($where,$paging){
        $count = $this->where($where)->count();
        $total = ceil($count/$paging['num']);
        $res = $this->where($where)->order('id','desc')->limit(($paging['current_page']-1)*$paging['num'],$paging['num'])->select();
        $pageInfo = [
            "current_page"=>$paging['current_page'],
            "total"=>$total
        ];
        $result = [
            'pageInfo' =>$pageInfo,
            'list' => $res
        ];
        return $result;
    }
    public function getDetailById($id){
        $where = [
            "id"=>$id
        ];
        $result = $this->where($where)->find();
        return $result;
    }
    public function updateNotice($id,$data){
        $where = [
            "id"=>$id,
        ];
        $result = $this->where($where)->find()->save($data);
        return $result;
    }
    public function removeNotice($id){
        $where = [
            'id'=>$id
        ];
        $result = $this->where($where)->delete();
        return $result;
    }
    public function getLimitListByWhere($where,$num){
        $result = $this->where($where)
            ->order('sort')
            ->field('id,title')
            ->limit($num)
            ->select();
        return $result;
    }
}